package de.koller.million.card.frame;

import de.koller.million.card.CNode;
import de.koller.million.card.CardStart;
import de.koller.million.card.commands.RemoveCNode;
import de.koller.million.card.engine.enc.Encounter;
import de.koller.million.card.frame.enc.EncounterFrame;
import de.koller.million.card.frame.item.ItemToolTip;
import de.koller.aframe.util.thread.ICommand;
import java.text.DecimalFormat;

public class CMainFrame extends CNode {

	public final PlayerChooseModel playerBoxModel = new PlayerChooseModel( this );
	private CMainFrameComp comp;

	@Override
	protected void init() {
		super.init();
		
		comp = new CMainFrameComp( this );
		
		comp.setTitle( CardStart.TITLE );
		comp.setVisible( true );
	}

	@Override
	protected void update( int elapsedTime ) {
		super.update( elapsedTime );
		
		comp.setTitle( CardStart.TITLE + " [" + (new DecimalFormat( "###,###,###,###,###,###" ).format( croot.getTime() )) + "]" );
		comp.encstarter.setText( croot.engine.getEncounter() != null ? "öffne Encounter" : "starte Encounter" );
		playerBoxModel.update();
	}

	@Override
	protected void release() {
		super.release();
		
		comp.dispose();
	}
	
	public void createNewPlayer() {
		croot.call( new ICommand() {
			@Override public void handle( Object... params ) {
				croot.engine.playerContainer.createNewPlayer( -1 );
			}
		});
	}
	
	public void save() {
		croot.call( new ICommand() {
			@Override public void handle( Object... params ) {
				croot.save();
			}
		} );
	}
	
	public void startEncounter() {
		croot.call( new ICommand() {
			@Override public void handle( Object... params ) {
				if( croot.engine.getEncounter() == null ) {
					Encounter e = new Encounter();
					croot.engine.setEncounter( e );
					
					croot.engine.getEncounter().view = new EncounterFrame( e );
					addChild( croot.engine.getEncounter().view );
				} else {
					if( croot.engine.getEncounter().view != null )
						croot.engine.getEncounter().view.comp.requestFocus();
					else {
						croot.engine.getEncounter().view = new EncounterFrame( croot.engine.getEncounter() );
						addChild( croot.engine.getEncounter().view );
					}
				}
			}
		} );
	}
	
	public void closeThis() {
		croot.call( new RemoveCNode( this ) );
		croot.stop();
		ItemToolTip.SINGLE.dispose();
	}
	
}
